/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/* 
 * Copyright (c) 2009 City University of Hong Kong
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License m_version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Author: Wilson Thong (wilsonwk@ee.cityu.edu.hk) 
 */
#ifndef BASE_GENERATOR_H
#define BASE_GENERATOR_H

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
#include "ns3/node-container.h"
#include "ns3/net-device-container.h"
#include "ns3/point-to-point-channel.h"
#include "ns3/point-to-point-net-device.h"

namespace ns3 {
namespace eecityu {

using namespace std;
using namespace ns3;

/**
 * \brief The base-class for all topology
 *
 * A new topology class should extends from this BaseTopology in order to leverage
 * all the basic functionalities and supports provided to a topoology class
 *
 * Currently, only point-to-point channel and point-to-point net-device is used
 * to implement a topology.
 */
class BaseTopology : public ObjectBase
{
public:
  static TypeId GetTypeId (void);
  virtual TypeId GetInstanceTypeId (void) const;

  enum ArcValueType {
    /// Number of packets traversed on an arc
    NPackets,
    /// The propagation delay of an arc
    Delay,
    /// The queue depth of a net device attaching on an arc
    QueueDepth,
  };

  BaseTopology (void);
  virtual ~BaseTopology (void);

public:  
  /**
   * \brief Read the BRITE topology data in NS-2 format
   *
   * \param filename The path and/or filename of the topology data
   * \return Number of uni-direction arcs created
   */
  uint32_t ReadBriteNs (string filename);
  /**
   * \brief Print out the topology in the format that Pajek understands
   *
   * Each point-to-point link is printed as two arcs, and the value (or
   * weight) of each arc is obtained by calling the GetArcValue () method
   *
   * \return A string describing the topology in the Pajek syntax
   */
  string PrintPajek () const;
  /**
   * \brief Get the number of nodes
   */
  uint32_t GetNNodes () const;
  /**
   * \brief Get the number of links
   */
  uint32_t GetNLinks () const;
  /**
   * \returns The set of BMSN's m_nodes after it is generated. 
   */
  NodeContainer& GetNodeContainer (void);
  /**
   * \returns The set of BMSN's m_nodes after it is generated. 
   */
  const NodeContainer& GetNodeContainer (void) const;
  /**
   * \returns The set of BMSN's node m_devices after it is generated.
   */
  NetDeviceContainer& GetNetDeviceContainer (void);
  /**
   * \returns The set of BMSN's node m_devices after it is generated.
   */
  const NetDeviceContainer& GetNetDeviceContainer (void) const;

protected:
  /// contain all nodes of the topology
  NodeContainer m_nodes;
  /// contains all net devices in the topology
  NetDeviceContainer m_devices;

private:
  /// Select an arc type to use when printing a Pajek topology
  ArcValueType m_pajekArcType;
  /**
   * \brief Fast forward an input file stream to the next line where arc/link is defined
   * \param is The input file stream object that is to be fast forwarded
   * \return The line that jumpped onto
   */
  string JumpToNextNsLine (istream& is);
  /**
   * \brief Get the value for each arc when outputing topology to Pajek
   *
   * If other topology has its own definition of arc value, this
   * method can be overloaded.
   *
   * \param dev The net-device being queried
   * \return The arc value
   */
  virtual string GetArcValue (Ptr<NetDevice> dev) const;
  /**
   * \brief Get the number of packets traversed on this arc
   *
   * \param dev The net-device being queried
   * \return The number of packets traversed throught the net-device
   */
  string GetArcNPackets (Ptr<NetDevice> dev) const;
  /**
   * \brief Return the line propagation delay as the arc value when
   * outputing topology to Pajek
   *
   * \param dev The net-device being queried
   * \return the line propagation delay attached to this net device
   */
  string GetArcDelay (Ptr<NetDevice> dev) const;
  /**
   * \brief Get the queue depth as the value for an arc
   *
   * \param dev The net-device being queried
   * \return The qeueue depth of the net device being queried
   */
  string GetArcQueueDepth (Ptr<NetDevice> dev) const;
  /**
   * \brief Get a node position for printing a Pajek topology
   *
   * The method returns a string of node location in the form [x y z]
   * as defined in Pajek documentation. The default behavior of this
   * method is return a null string. Other topology class can overload
   * this method to return a meaningful node position
   *
   * \param n The node whose position is consulted
   * \return The string describing the node position and compiled
   * to the Pajek syntax
   */
  virtual string GetNodePosition (Ptr<Node> n) const;
};
ostream& operator<< (ostream& os, const BaseTopology& bt);

} // namespace eecityu
} // namespace ns3


#endif // #ifndef BASE_GENERATOR_H
